package com.hangsu.train.components.web.controller;

import com.hangsu.train.components.entity.Task;
import com.hangsu.train.components.params.TaskParam;
import com.hangsu.train.components.service.TaskService;
import com.hangsu.train.core.result.Resp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 任务列表
     */
    @PostMapping("/list")
    public Resp<?> list(@RequestBody TaskParam param) throws Exception {
        Page<Task> tasks = taskService.list(param);
        return Resp.ok(tasks);
    }

    /**
     * 任务创建
     */
    @PostMapping("/create")
    public Resp<?> create(@RequestBody Task param) throws Exception {
        taskService.create(param);
        return Resp.ok();
    }

    /**
     * 任务修改
     */
    @PostMapping("/update")
    public Resp<?> update(@RequestBody Task param) throws Exception {
        taskService.update(param);
        return Resp.ok();
    }

    /**
     * 任务删除
     */
    @PostMapping("/del")
    public Resp<?> del(@RequestBody Task param) throws Exception {
        taskService.removeJob(String.valueOf(param.getId()));
        taskService.del(param.getId());
        return Resp.ok();
    }

}